package com.ruoqing.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.domain.rquser.UserEntity;
import com.ruoqing.core.dto.BaseResponse;
import com.ruoqing.core.dto.auth.LoginUserDto;
import com.ruoqing.core.dto.user.UserRegisterDto;
import com.ruoqing.core.exception.UserException;
import com.ruoqing.core.fallback.downgrade.UserDowngradeMethods;
import com.ruoqing.core.fallback.fuse.UserLimitingMethods;
import com.ruoqing.core.vo.auth.AuthResponseVo;
import com.ruoqing.core.vo.inform.SmsResponseVo;
import com.ruoqing.user.service.api.UserRegisterService;
import com.ruoqing.user.service.api.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
	* @author YaoXian
	* @title: UserController
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 用户服务 - 用户服务控制层接口
	* @date 2020-11-04 16:10
	*/
@RestController
@RequestMapping(path = "/user")
@Api(tags = "用户服务对外HTTP调用 - 控制层接口")
public class UserController {

		@Resource
		private UserService userService;

		@Resource
		private UserRegisterService userRegisterService;

		//@BaseLog(title = "用户登录",module = ModuleConstants.APP_AUTH,businessType = BusinessType.GRANT,operatorType = OperatorType.USER)
		@PostMapping(path = "/login")
		public AuthResponseVo userLogin(@RequestBody LoginUserDto loginUserDto){
				System.out.println(loginUserDto);
				return userRegisterService.userLogin(loginUserDto);
		}
		
		//@BaseLog(title = "用户注册",module = ModuleConstants.APP_USER,businessType = BusinessType.INSERT,operatorType = OperatorType.USER)
		@ApiOperation(value = "若兮用户注册", httpMethod = "POST")
		@PostMapping(path = "/register")
		@ApiImplicitParam(name = "UserRegisterDto", value = "用户注册信息"
						, required = true, dataType = "UserRegisterDto")
		public BaseResponse userRegister(@RequestBody UserRegisterDto userRegisterDto) {
				return userRegisterService.userRegister(userRegisterDto);
		}

		@ApiOperation(value = "发送注册手机验证码", httpMethod = "GET")
		@GetMapping(path = "/sendPhoneCode")
		@ApiImplicitParam(name = "phoneNumber", value = "用户手机号"
						, required = true, dataType = "String")
		@SentinelResource(value = "sendPhoneCode",blockHandlerClass = UserLimitingMethods.class,blockHandler = "baseLimitingMethod",
						fallbackClass = UserDowngradeMethods.class,fallback = "baseDowngradeMethod")
		public SmsResponseVo sendPhoneCode(String phoneNumber) throws Exception{
				return userRegisterService.registerGetPhoneCode(phoneNumber);
		}

		@ApiOperation(value = "校验手机注册验证码", httpMethod = "GET")
		@GetMapping(path = "/checkRegisterCode")
		@ApiImplicitParams(value = {@ApiImplicitParam(name = "code", value = "手机验证码"
						, required = true, dataType = "String"),@ApiImplicitParam(name = "phoneNumber", value = "发送验证码的手机号"
						, required = true, dataType = "String")})
		public BaseResponse checkRegisterCode(String code,String phoneNumber){
				return userRegisterService.checkRegisterCode(code,phoneNumber);
		}

		@ApiOperation(value = "查询所有用户", httpMethod = "GET")
		@GetMapping(path = "/findAllUsers")
		public List<UserEntity> findAllUsers(){
				return userService.findAllUsers();
		}

		@SentinelResource(value = "testSentinel",blockHandlerClass = UserLimitingMethods.class,blockHandler = "baseLimitingMethod",
		fallbackClass = UserDowngradeMethods.class,fallback = "baseDowngradeMethod")
		@GetMapping(path = "/testSentinel")
		public BaseResponse testSentinel(){
				try {
						Thread.sleep(300);
				} catch (InterruptedException e) {
						e.printStackTrace();
				}
				return BaseResponse.buildResponse(Constants.responseOk,"测试分布式流量防卫兵Sentinel!");
		}

		@GetMapping(path = "/test")
		@ApiOperation(value = "测试服务限流熔断降级", httpMethod = "GET")
		public BaseResponse test(){
				try {
						int i = 1/0;
				}catch (Exception e){
						throw new UserException(Constants.responseError,"用户服务内部异常!");
				}
				return BaseResponse.buildResponse(Constants.responseOk);
		}
	}
